/* <pre>
 * Copyright (c) The Loose Coupling Team.
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 *
 *      http://www.apache.org/licenses/LICENSE-2.0 
 *
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 *</pre>
 */

package org.lc.eic.messaging.requestreply;

import javax.jms.JMSException;

import org.lc.eic.api.requestreply.sync.Replier;
import org.lc.eic.core.pipesandfilters.impl.GZIPCompressDataFilter;
import org.lc.eic.core.pipesandfilters.impl.GZIPUncompressDataFilter;
import org.lc.eic.messageconstruct.jaxb.content.ContentMapper;
import org.lc.eic.messageconstruct.requestreply.jaxb.impl.JAXBReplySerializer;
import org.lc.eic.messageconstruct.requestreply.jaxb.impl.JAXBRequestSerializer;
import org.lc.eic.messaging.assembly.DeserializeRequestFilter;
import org.lc.eic.messaging.assembly.EnvelopeUnwrapReplier;
import org.lc.eic.messaging.assembly.ReceiveDataPipe;
import org.lc.eic.messaging.assembly.SendDataPipe;
import org.lc.eic.messaging.assembly.SerializeReplyFilter;
import org.lc.eic.messagingsystem.activemq.ActiveMQMessagingSystem;
import org.lc.eic.messagingsystem.exception.RegistrationException;
import org.lc.eic.messagingsystem.exception.StartMessagingException;
import org.lc.eic.messgeconstruct.requestreply.Reply;
import org.lc.eic.messgeconstruct.requestreply.Request;
import org.lc.eic.synchronizer.replier.ReplierSynchronizer;



/**
 *  TODO: Add type comment here!!
 *	@author <a href="mailto:p.lycke@googlemail.com">PaLy</a>
 *
 * 
 */
public class ReplierHandler<I, O>  {

	
	
	
	
	public ReplierHandler(String activeMQUrl, String receiveRequestQueue, String sendReplyQueue, ContentMapper contentMapper, Replier<I, O> replier){
		
		//Create the components
		ActiveMQMessagingSystem activeMQMessagingSystem = null;
		try {
			activeMQMessagingSystem = new ActiveMQMessagingSystem(activeMQUrl);
		} catch (JMSException e) {
			throw new RuntimeException(e);
		}
		
		
		ReceiveDataPipe receiveRequestPipe = new ReceiveDataPipe();
		SendDataPipe sendReplyPipe = new SendDataPipe();
		
		DeserializeRequestFilter<I> deserializeRequestFilter = new DeserializeRequestFilter<I>();
		SerializeReplyFilter<O> serializeReplyFilter = new SerializeReplyFilter<O>();
		
		JAXBRequestSerializer<I> jaxbRequestSerializer = new JAXBRequestSerializer<I>();
		JAXBReplySerializer<O> jaxbReplySerializer = new JAXBReplySerializer<O>();
		
		ReplierSynchronizer<Request<I>, Reply<O>> replierSynchronizer = new ReplierSynchronizer<Request<I>, Reply<O>>();
		
		EnvelopeUnwrapReplier<I, O> envelopeUnwrapReplier = new EnvelopeUnwrapReplier<I, O>();
		
		//Assemble them
		
		try {
			activeMQMessagingSystem.registerInboundEndpoint(receiveRequestPipe, receiveRequestQueue);
			activeMQMessagingSystem.registerOutboundEndpoint(sendReplyPipe, sendReplyQueue);
		} catch (RegistrationException e) {
			throw new RuntimeException(e);
		}
		
		receiveRequestPipe.setInboundPipe(new GZIPUncompressDataFilter(deserializeRequestFilter));
		serializeReplyFilter.setOutboundPipe(new GZIPCompressDataFilter(sendReplyPipe));
		
		
		deserializeRequestFilter.setRequestSerializer(jaxbRequestSerializer);
		serializeReplyFilter.setReplySerializer(jaxbReplySerializer);
		
		jaxbRequestSerializer.setContentMapper(contentMapper);
		jaxbReplySerializer.setContentMapper(contentMapper);
		
		deserializeRequestFilter.setOutboundPipe( replierSynchronizer.getRequestPipe());
		replierSynchronizer.setReplyPipe(serializeReplyFilter);
		
		replierSynchronizer.setRequestDispatcher(envelopeUnwrapReplier);
		
		envelopeUnwrapReplier.setReplier(replier);
		
		try {
			activeMQMessagingSystem.startMessaging();
		} catch (StartMessagingException e) {
			throw new RuntimeException(e);
		}
		
		
		
	}

	
	

	
}
